package com.ruoyi.fun.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.fun.domain.EcAdvertisement;
import com.ruoyi.fun.domain.EcProduct;
import com.ruoyi.fun.service.IEcAdvertisementService;
import com.ruoyi.fun.service.IEcProductService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 广告列Controller
 * 
 * @author ruoyi
 * @date 2025-08-08
 */
@RestController
@RequestMapping("/fun/advertisement")
public class EcAdvertisementController extends BaseController
{
    @Autowired
    private IEcAdvertisementService ecAdvertisementService;
    
    @Autowired
    private IEcProductService ecProductService;

    /**
     * 查询广告列列表
     */
    @PreAuthorize("@ss.hasPermi('fun:advertisement:list')")
    @GetMapping("/list")
    public TableDataInfo list(EcAdvertisement ecAdvertisement)
    {
        startPage();
        Long orgId = getOrgId();
        // 103组织拥有不过滤的权限
        if (orgId != null && orgId == 103L) {
            orgId = null;
        }
        ecAdvertisement.setOrgId(orgId);
        List<EcAdvertisement> list = ecAdvertisementService.selectEcAdvertisementList(ecAdvertisement);
        return getDataTable(list);
    }

    /**
     * 导出广告列列表
     */
    @PreAuthorize("@ss.hasPermi('fun:advertisement:export')")
    @Log(title = "广告列", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, EcAdvertisement ecAdvertisement)
    {
        List<EcAdvertisement> list = ecAdvertisementService.selectEcAdvertisementList(ecAdvertisement);
        ExcelUtil<EcAdvertisement> util = new ExcelUtil<EcAdvertisement>(EcAdvertisement.class);
        util.exportExcel(response, list, "广告列数据");
    }

    /**
     * 获取广告列详细信息
     */
    @PreAuthorize("@ss.hasPermi('fun:advertisement:query')")
    @GetMapping(value = "/{adId}")
    public AjaxResult getInfo(@PathVariable("adId") Long adId)
    {
        Long orgId = getOrgId();
        // 103组织拥有不过滤的权限
        if (orgId != null && orgId == 103L) {
            orgId = null;
        }
        return success(ecAdvertisementService.selectEcAdvertisementByAdIdAndOrgId(adId, orgId));
    }

    /**
     * 新增广告列
     */
    @PreAuthorize("@ss.hasPermi('fun:advertisement:add')")
    @Log(title = "广告列", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody EcAdvertisement ecAdvertisement)
    {
        ecAdvertisement.setOrgId(getOrgId());
        return toAjax(ecAdvertisementService.insertEcAdvertisement(ecAdvertisement));
    }

    /**
     * 修改广告列
     */
    @PreAuthorize("@ss.hasPermi('fun:advertisement:edit')")
    @Log(title = "广告列", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody EcAdvertisement ecAdvertisement)
    {
        return toAjax(ecAdvertisementService.updateEcAdvertisement(ecAdvertisement));
    }

    /**
     * 删除广告列
     */
    @PreAuthorize("@ss.hasPermi('fun:advertisement:remove')")
    @Log(title = "广告列", businessType = BusinessType.DELETE)
	@DeleteMapping("/{adIds}")
    public AjaxResult remove(@PathVariable Long[] adIds)
    {
        return toAjax(ecAdvertisementService.deleteEcAdvertisementByAdIds(adIds));
    }
    
    /**
     * 获取商品列表（用于广告选择商品）
     */
    @PreAuthorize("@ss.hasPermi('fun:advertisement:list')")
    @GetMapping("/productList")
    public TableDataInfo getProductList(EcProduct ecProduct)
    {
        startPage();
        List<EcProduct> list = ecProductService.selectEcProductList(ecProduct);
        return getDataTable(list);
    }
    /**
     * 根据adId和组织ID获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('fun:ecadvertisement:query')")
    @GetMapping(value = "/{adId}/org/{orgId}")
    public AjaxResult getInfoByOrgId(@PathVariable("adId") Long adId, @PathVariable("orgId") Long orgId)
    {
        return success(ecAdvertisementService.selectEcAdvertisementByAdIdAndOrgId(adId, orgId));
    }

    /**
     * 根据组织ID查询列表
     */
    @PreAuthorize("@ss.hasPermi('fun:ecadvertisement:list')")
    @GetMapping("/org/{orgId}")
    public TableDataInfo listByOrgId(@PathVariable Long orgId)
    {
        startPage();
        EcAdvertisement ecAdvertisement = new EcAdvertisement();
        List<EcAdvertisement> list = ecAdvertisementService.selectEcAdvertisementListByOrgId(ecAdvertisement, orgId);
        return getDataTable(list);
    }


}